
package com.chasekernan.hxnova.client;

import swhx.Connection;
import haxe.remoting.Context;

import flash.display.Loader;
import flash.system.LoaderContext;
import flash.system.ApplicationDomain;
import flash.errors.IOError;
import flash.events.IOErrorEvent;
import flash.net.URLRequest;
import flash.events.Event;
import flash.display.StageScaleMode;
import flash.utils.SetIntervalTimer;

enum AswingSkin {
    Blue;
    Chrome;
	Zhao;
}

class ClientBase {
    
    public static var ASWING_LIBRARY    = "aswing.swf";
    public static var DEFAULT_SKIN      = Zhao;
    
    private static var INSTANCE         : ClientBase;
    
    private var connection              : Connection;
    private var context                 : Context;
    
    public static function getInstance() : ClientBase {
        if (INSTANCE == null) INSTANCE = new ClientBase();
        
        return INSTANCE;
    }
    
    private function new() {
        context     = new Context();
        connection  = Connection.desktopConnect(context);
        setupContext();
        
        flash.Lib.current.stage.scaleMode = StageScaleMode.NO_SCALE;
    }
    
    private inline function setupContext() {
        context.addObject("ClientBase", this);
    }
    
    public function addCommunicationObject(name : String, value : Dynamic) {
        context.addObject(name, value);
    }
    
    public function getConnection() : Connection {
        return connection;
    }
    
    public function loadAswing(?onComplete : Void -> Void) {
        var me              = this;
        var loader          = new Loader();
        var loaderContext   = new LoaderContext(false, 
                                    ApplicationDomain.currentDomain);
        loader.load(new URLRequest(ASWING_LIBRARY), loaderContext);
        
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE,
            function(?e : Dynamic) {
                me.setAswingSkin(DEFAULT_SKIN);
                
                if (onComplete != null) onComplete();
            }
        );
        
        loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,
            function(?e : Dynamic) {
                me.connection.SwhxBase.showMessage.call(["Error",
                    "Failed to load aswing library: " + e]);
            }
        );
    }
    
    public function setAswingSkin(skin : AswingSkin) {
        var path        = switch(skin) {
            case Blue:      "com.alvasun.laf.blue.BluenessLAF";
            case Chrome:    "com.alvasun.laf.chrome.ChromeLAF";
			case Zhao:		"org.aswing.ppzhao.PPZhaoLAF";
        };
        
        var laf         = ApplicationDomain.currentDomain.getDefinition(path);
        
        path            = "org.aswing.UIManager";
		var UIManager   = ApplicationDomain.currentDomain.getDefinition(path);
        
        path            = "org.aswing.AsWingUtils";
		var AsWingUtils = ApplicationDomain.currentDomain.getDefinition(path);
        
		UIManager.setLookAndFeel(Type.createInstance(laf, []));
		AsWingUtils.updateAllComponentUIInMemory();
    }
    
    
}